package com.tfy.liquor.dao;

import com.tfy.liquor.bean.ProductInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 产品信息
 */
@Mapper
@Service
public interface ProductInfoDao {
    // 添加产品信息
    @Insert("insert into product_info values" +
            "('${id}', ${categoryId}, ${vendorId}," +
            "'${productEncode}', '${image}', " +
            "'${productName}', ${purchasePrice}, " +
            "${salesPrice}, '${unit}', ${inventory}, " +
            "${shelfLife}, '${description}', ${sort}, " +
            "${status}, '${createTime}')")
    int addProductInfo(ProductInfo productInfo);

    // 修改产品信息
    @Insert("update product_info set " +
            "categoryId=${categoryId}, " +
            "vendorId=${vendorId}, " +
            "productEncode='${productEncode}', " +
            "image='${image}', " +
            "productName='${productName}', " +
            "purchasePrice=${purchasePrice} " +
            "salesPrice=${salesPrice}, " +
            "unit='${unit}', " +
            "inventory=${inventory}, " +
            "shelfLife=${shelfLife}, " +
            "description='${description}', " +
            "sort=${sort}, status=${status} where id='${id}'")
    int editProductInfo(ProductInfo productInfo);

    // 查询产品列表
    @Select("<script>" +
            "select * from product_info " +
            "where 1=1" +
            "<if test='id != null'>and id = '${id}'</if>" +
            "<if test='categoryId != null'>and categoryId = '${categoryId}'</if>" +
            "<if test='productEncode != null'>and productEncode = '${productEncode}'</if>" +
            "<if test='productName != null'>and productName = '${productName}'</if>" +
            "order by id desc" +
            "</script>")
    ArrayList<ProductInfo> getProductInfo(ProductInfo productInfo);

    // app端首页展示列表
    @Select("select id, image, productName, salesPrice from product_info where status=0 order by sort desc")
    ArrayList<ProductInfo> getIndexProductInfo();

    // 根据产品编号获取产品信息
    @Select("select * from product_info where id = '${id}' and status=0")
    ProductInfo getProductInfoByProductId(String id);

    // 根据商品名称查询
    @Select("select * from product_info where productName like '%${productName}%' and status=0")
    ArrayList<ProductInfo> getProductInfoByProductName(String productName);
}
